'use strict';

var webappApp = angular.module('webappApp', ['ngResource', 'ui'])
  .config(['$routeProvider', function($routeProvider) {
    $routeProvider
      .when('/', {
        templateUrl: 'views/search.html',
        controller: 'SearchCtrl'
      })
      .when('/s/:id', {
        templateUrl: 'views/spot.html',
        controller: 'SpotCtrl'
      })
      .when('/create', {
        templateUrl: 'views/create.html',
        controller: 'CreateCtrl'
      })
      .otherwise({
        redirectTo: '/'
      });
  }])
  .value('ui.config', {
    jq: {
      modal: {
        keyboard: false,
        backdrop: 'static',
        show: false
      }
    },
    map: {
      center: new google.maps.LatLng(0, 0),
      zoom: 0,
      mapTypeId: google.maps.MapTypeId.ROADMAP,
      draggable: true,
      panControl: false,
      streetViewControlOptions: {
        position: google.maps.ControlPosition.LEFT_BOTTOM
      },
      mapTypeControlOptions: {
        position: google.maps.ControlPosition.RIGHT_BOTTOM
      },
      zoomControl: false
    }
  })
  .run(function() {
    Object.size = function (obj) {
      var size = 0, key;
      for (key in obj) {
          if (obj.hasOwnProperty(key)) size++;
      }
      return size;
    };

    if ( !Array.prototype.forEach ) {
      Array.prototype.forEach = function(fn, scope) {
        for(var i = 0, len = this.length; i < len; ++i) {
          fn.call(scope, this[i], i, this);
        }
      };
    }

    if(!Array.prototype.indexOf) {
      Array.prototype.indexOf = function(what, i) {
        i = i || 0;
        var L = this.length;
        while (i < L) {
          if(this[i] === what) return i;
          ++i;
        }
        return -1;
      };
    }
    
    Array.prototype.remove = function() {
      var what, a = arguments, L = a.length, ax;
      while (L && this.length) {
        what = a[--L];
        while ((ax = this.indexOf(what)) !== -1) {
          this.splice(ax, 1);
        }
      }
      return this;
    };
  })
  .factory('webRoot', function ($location) {
    return '//' + $location.host() + '\\:8888/rest';
  });
